﻿@{
    ViewBag.Title = "PlanIndex";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>

    var Data = [];
    var tArray = new Array();//数据坐标
    var Col = 52; //页面显示列数
    var Gap = 0;  //列间隙
    var height = 30; //行高
    var W = 20; //单元宽度

    var dArray = new Array();
    var aArray = new Array();
    var Weeks = "";
    var SWeek = "";
    var WeekBillData = [];
    var Type = "2", year = "", month = "", week = "";
    var x = "", y = "";
    var ok = false, bool = false;
    var BeginDate = "", EndDate = "";
    var WorkId = "";
    var WorkDetail = "";
    document.addEventListener('mouseup', Up);
    var organizeid = " @LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId";
    $(function () {
        GetTime();
        GetTitel();
        GetWeekBillData();
        InitialPage();
        GetDynamicCols();
        GetTree();
        $(".bbit-tree-node-el").mousedown(function (e) {
            var s = $(this).children().eq(3).children();
            WorkId = s.attr("data-value");
            if ($(this).children().eq(2).attr("class") == "fa fa-file-word-o") {
                if (e.which == "1") {
                    ok = true;
                    bool = true;
                    $("#HZ").html(s.html());
                    e = e ? e : window.event;
                    x = e.screenX;
                    var screen = window.screen.width;
                    var l = parseInt(x - $(this).offset().left - 80);
                    SetData(l);
                }
            }

        })
    })

    function GetTree() {
        var item = {
            height: $(window).height() - 55.5,
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/GetTreeJson",
            onnodeclick: function (item) {

            }
        };
        //初始化
        $("#itemTree1").treeview(item);
    }
    function GetWeekBillData() {
        var queryJson = {};
        queryJson["Type"] = Type;
        queryJson["year"] = year;
        queryJson["month"] = month;
        queryJson["week"] = week;
        queryJson["OrganizeId"] = organizeid;
        $.ajax({
            type: "post",
            dataType: "json",
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/GetWeekBillData",
            data: { queryJson: JSON.stringify(queryJson) },
            success: function (d) {

                WeekBillData = [];
                for (var i = 0; i < d.length; i++) {
                    WeekBillData.push(d[i]);

                }
                if (Type == "1") {
                    ChangeData();
                }
                if (Type == "2") {
                    ChangeMonthData();
                }
                if (Type == "3") {
                    ChangeWeekData();
                }
                GetAutoLayoutData();
                AutoLayout();
            }
        });
    }
    function SetData(l) {
        document.onmousemove = function (e) {
            if (bool) {
                e = e ? e : window.event;
                x = e.pageX;
                y = e.pageY;
                var div = document.getElementById('HZ');
                div.style.left = parseInt(e.screenX - l - 80) + "px";
                div.style.top = y + "px";
                div.style.visibility = "visible";
                //  $("#CC").html(e.pageX + "--" + e.pageY + "--" + $('.gridPanel').offset().left);
                var i = 1;
                if (!ok) {
                    $(".cs_dv").each(function () {
                        var dvx = $(this).offset().left;
                        var dvy = $(this).offset().top;
                        var width = $(this).width();
                        var L = $(".ui-layout-west").width();
                        var str = $("#HZ").html();
                        var s = x;

                        if (dvx <= s && s <= dvx + width && y >= dvy) {
                            if (Type == 1) {
                                var d = parseInt(i + SWeek - 1);
                                GetSETime(year, d, 1);
                                var Id = new Date().getTime();
                                $("#HZ").css({ "visibility": "hidden" });
                                WeekBillData.push({ Id: Id, ItemId: WorkId, x: parseInt(i + SWeek - 1), w: 1, Title: str, Year: year, Month: month, State: "0", IsEdit: 0, BeginDate: BeginDate, EndDate: EndDate, WorkDetail: WorkDetail });
                                ChangeData();
                                GetAutoLayoutData();
                                AutoLayout()
                                $(this).css({ "background-color": "#fff" });
                                ok = false;
                                bool = false;
                            }
                            if (Type == 2) {
                                var d = parseInt(i + SWeek - 1);
                                GetSETime(year, d, 1);
                                var Id = new Date().getTime();
                                $("#HZ").css({ "visibility": "hidden" });
                                WeekBillData.push({ Id: Id, ItemId: WorkId, x: parseInt(i + SWeek - 1), w: 1, Title: str, Year: year, Month: month, State: "0", IsEdit: 0, BeginDate: BeginDate, EndDate: EndDate, WorkDetail: WorkDetail });
                                ChangeMonthData();
                                GetAutoLayoutData();
                                AutoLayout()
                                $(this).css({ "background-color": "#fff" });
                                ok = false;
                                bool = false;
                            }
                            if (Type == 3) {
                                var d = week;
                                getDayOfWeek(year, d, parseInt(i - 1), 1);
                                var Id = new Date().getTime();
                                $("#HZ").css({ "visibility": "hidden" });
                                WeekBillData.push({ Id: Id, ItemId: WorkId, x: d, w: 1, Title: str, Year: year, Month: month, State: "0", IsEdit: 0, BeginDate: BeginDate, EndDate: EndDate, WorkDetail: WorkDetail });
                                ChangeWeekData();
                                GetAutoLayoutData();
                                AutoLayout()
                                $(this).css({ "background-color": "#fff" });
                                ok = false;
                                bool = false;
                            }

                        }
                        else {
                            $("#HZ").css({ "visibility": "hidden" });
                            ok = false;
                            bool = false;
                        }
                        i++;
                    })
                }
                else {
                    $(".cs_dv").each(function () {
                        var dvx = $(this).offset().left;
                        var dvy = $(this).offset().top;
                        var width = $(this).width();
                        var L = $(".ui-layout-west").width();
                        var str = $("#HZ").html();

                        var s = x;
                        if (dvx <= s && s <= dvx + width && y >= dvy) {
                            $(this).css({ "background-color": "#A2DEF0" });
                        }
                        else {
                            $(this).css({ "background-color": "#fff" });
                        }

                    })
                }
            }

        }
    }



    function GetDynamicCols() {
        $('#gridTable').GridUnload();
        $.ajax({
            type: "GET",
            async: false,
            dataType: "json",
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/GetColumns?Type=" + Type + "&year=" + year + "&month=" + month + "&week=" + week,
            success: function (data) {
                //
                Weeks = data.WeeksData;
                SWeek = data.SWeekData;
                creategrid(data);
                GetBG_TAb();
            }
        });
    }
    //加载表格
    function creategrid(data) {
        //
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        $gridTable.jqGrid({
            datatype: 'json',
            height: $(window).height() - 150,
            autowidth: true,
            colModel: data.ColMs,
            viewrecords: true,
            hoverrows: false,
            sortname: 'Date',
            sortorder: 'desc',
            rownumbers: false,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
                $("#gridTable tr").css({ "background": "#ffffff" });
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
                W = $(".cs_dv").width(); //单元宽度
            }
        });
        $gridTable.jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders: data.Headdata
        });
        $gridTable[0].addJSONData(data.Coldata);

    }
    function GetTime()//初始化时间
    {
        var myDate = new Date();
        var totalDays = 0;
        years = myDate.getYear()
        if (years < 1000)
            years += 1900
        var days = new Array(12);
        days[0] = 31; days[2] = 31; days[3] = 30; days[4] = 31; days[5] = 30;
        days[6] = 31; days[7] = 31; days[8] = 30; days[9] = 31; days[10] = 30; days[11] = 31;

        if (Math.round(myDate.getYear() / 4) == myDate.getYear() / 4) {  //判断是否为闰年，针对2月的天数进行计算
            days[1] = 29
        } else {
            days[1] = 28
        }

        if (myDate.getMonth() == 0) {
            totalDays = totalDays + myDate.getDate();
        } else {
            var curMonth = myDate.getMonth();
            for (var count = 1; count <= curMonth; count++) {
                totalDays = totalDays + days[count - 1];
            }
            totalDays = totalDays + myDate.getDate();
        }
        year = myDate.getFullYear();  //获取当前年
        month = myDate.getMonth() + 1;  //获取当前月
        week = Math.round(totalDays / 7);  //获取当第几周
    }
    function GetTitel() {
        if (Type == "1") $("#Time").html(year + "年");
        if (Type == "2") $("#Time").html(year + "年" + month + "月");
        if (Type == "3") $("#Time").html(year + "年 W" + week);
    }

    function btn_up() //上一页
    {
        if (Type == "1") {
            year--;
            GetWeekBillData();
            GetTitel();
            GetDynamicCols();
            ChangeData();
        }
        if (Type == "2") {
            if (month == 1) {
                year--;
                GetWeekBillData();
                month = 12;
            } else {
                --month;
            }
            GetTitel();

            GetDynamicCols();
            ChangeMonthData();

        }
        if (Type == "3") {
            if (week == 1) {
                year--;
                GetWeekBillData();
                month = 12;
                week = 52;
            } else {
                week--;
            }
            GetTitel();
            GetDynamicCols();
            ChangeWeekData();
        }

        GetAutoLayoutData();
        AutoLayout();
        GetBG_TAb();

    }
    function btn_down()//下一页
    {
        if (Type == "1") {
            year++;
            GetWeekBillData();
            GetTitel();
            GetDynamicCols();
            ChangeData();
        }
        if (Type == "2") {
            if (month == 12) {
                year++;
                GetWeekBillData();
                month = 1;
            }
            else {
                ++month;

            }
            GetTitel();
            GetDynamicCols();
            ChangeMonthData();
        }
        if (Type == "3") {
            if (week == 52 && month != 12) {
                month++;
                GetWeekBillData();
                week = 1;
            }
            if (week == 52 && month == 12) {
                year++;
                GetWeekBillData();
                month = 1;
                week = 1;
            }
            else {
                week++;
            }
            GetTitel();
            GetDynamicCols();
            ChangeWeekData();
        }

        GetAutoLayoutData();
        AutoLayout();
        GetBG_TAb();
    }
    function GetBG_TAb() {
        $gridTable = $("#gridTable");
        $CS_T = $("#CS_T");
        $BG_Tab = $("#Tab");
        var L = $gridTable.offset().left;
        var T = $gridTable.offset().top;
        var W = $gridTable.width();
        var H = $CS_T.height();
        $CS_T.css({ "left": L + "px", "top": T + "px", width: W + "px" });
        $BG_Tab.css({ "left": L + "px", "top": T + H + "px" }); //, "height": H + "px", "width": W + "px"
    }


    function Change(Id) {

        $RDiv = $("#R" + Id);
        $div = $("#L" + Id);
        var L = parseInt($div.offset().left);
        var bool2 = true;
        var i = 1

        document.onmousemove = function (e) {
            $("#Tab").css({ "cursor": "ew-resize" })
            x = e.pageX;
            y = e.pageY;
            if (bool2) {
                $(".cs_dv").each(function () {
                    var dvx = $(this).offset().left;
                    var dvy = $(this).offset().top;
                    var width = $(this).width();
                    var s = x;
                    if (x > parseInt(L + width - 6)) {
                        var width = parseInt(x - L);
                        $div.css({ "width": width + "px", "z-index": 10000, "borderColor": "#7267BA" });

                    }
                })
                bool2 = true;
            }
            if (!ok) {
                if (i == 1) {
                    var aa = 1;
                    $(".cs_dv").each(function () {
                        var dvx = $(this).offset().left;
                        var dvy = $(this).offset().top;
                        var width = $(this).width();
                        var s = x;

                        if (dvx <= s && s <= dvx + width) {

                            var width = parseInt(x - L);
                            var q = width % W > 0 ? width / W : 1;
                            q = Math.ceil(q);
                            $div.css({ "width": width + "px" });
                            if (FindData(Id).State == "1") {
                                FindData(Id).State = "2";//修改
                            }
                            if (Type == 1) {
                                GetSETime(year, FindData(Id).x, q);

                                FindData(Id).w = q;
                                FindData(Id).EndDate = EndDate;
                                ChangeData();
                                GetAutoLayoutData();
                                AutoLayout();
                            }
                            if (Type == 2) {
                                GetSETime(year, FindData(Id).x, q);
                                var z = FindDataOfWeek(Id);

                                FindData(Id).w = q;
                                FindData(Id).EndDate = EndDate;
                                ChangeMonthData();
                                GetAutoLayoutData();
                                AutoLayout();
                            }
                            if (Type == 3) {
                                var a = aa - FindDataOfWeek(Id).x;
                                getDayOfWeek(year, week, 0, aa);
                                FindData(Id).EndDate = EndDate;
                                ChangeWeekData();
                                GetAutoLayoutData();
                                AutoLayout()
                            }

                            i++;
                        }

                        aa++;

                    })

                }
                bool2 = false;
                ok = true;

            }
        }

    }

    function FindData(Id) {

        for (var i = 0; i < WeekBillData.length; i++) {
            if (WeekBillData[i].Id == Id) {
                return WeekBillData[i];
            }
        }
    }
    function FindDataOfWeek(Id) {
        for (var i = 0; i < Data.length; i++) {
            if (Data[i].Id == Id) {
                return Data[i];
            }
        }
    }

    function Up() {
        ok = false;
    }
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            description: "==请选择所属机构==",
            height: "250px",
            click: function (data) {
                organizeid = data.id;
                GetWeekBillData();
            }
        });
        $("#OrganizeId").ComboBoxSetValue(organizeid);
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 171.5);
            }, 200);
            e.stopPropagation();
        });

        $("#CC").css({ "width": $('.gridPanel').width() / 2 - 400 + "px" });
        //点击看板
        $("#time_see a.btn-default").click(function () {
            $("#time_see a.btn-default").removeClass("active");
            $(this).addClass("active");
            Type = $(this).attr("data-value");
            $("#CS_T").html("");
            GetTime();
            if (Type == 1) {
                GetTitel();
                GetDynamicCols();
                ChangeData();
                GetAutoLayoutData();
                AutoLayout();
                GetBG_TAb();

            }
            if (Type == 2) {
                GetTitel();
                GetDynamicCols();
                ChangeMonthData();
                GetAutoLayoutData();
                AutoLayout();
                GetBG_TAb();
            }
            if (Type == 3) {
                GetTitel();
                GetDynamicCols();
                ChangeWeekData();
                GetAutoLayoutData();
                AutoLayout();
                GetBG_TAb();
            }


        });

    }
    function ChangeData() {
        //
        Data = [];
        for (var i = 0; i < WeekBillData.length; i++) {
            if (Type == 1) {
                Data.push({ Id: WeekBillData[i].Id, ItemId: WeekBillData[i].ItemId, x: WeekBillData[i].x, w: WeekBillData[i].w, Title: WeekBillData[i].Title, Content: WeekBillData[i].Content, State: WeekBillData[i].State, BeginDate: WeekBillData[i].BeginDate, EndDate: WeekBillData[i].EndDate })
            }
            else {
                if (WeekBillData[i].x >= SWeek && WeekBillData[i].x < SWeek + Weeks) {
                    Data.push({ Id: WeekBillData[i].Id, ItemId: WeekBillData[i].ItemId, x: WeekBillData[i].x - SWeek + 1, w: WeekBillData[i].w, Title: WeekBillData[i].Title, Content: WeekBillData[i].Content, State: WeekBillData[i].State, BeginDate: WeekBillData[i].BeginDate, EndDate: WeekBillData[i].EndDate })
                }
            }
        }

    }

    function ChangeMonthData() {
        Data = [];
        var Sdate = "";
        var Edate = "";
        var a = "";
        var b = "";
        var c = "";
        var d = "";
        var e = "";
        GetSETime(year, SWeek, Weeks);
        //
        var STime = new Date(BeginDate.replace(/-/g, "/"));
        var ETime = new Date(EndDate.replace(/-/g, "/"));
        for (var i = 0; i < WeekBillData.length; i++) {
            Sdate = new Date(WeekBillData[i].BeginDate.replace(/-/g, "/"));
            Edate = new Date(WeekBillData[i].EndDate.replace(/-/g, "/"));
            if ((Sdate >= STime && Sdate < ETime) || (Edate > STime && Edate <= ETime) || (Sdate < ETime && Edate > ETime) || (Sdate >= STime && Edate <= ETime)) {
                a = Math.round((Edate - Sdate) / 604800000);
                b = Math.round((STime - Sdate) / 604800000);
                c = Math.round((ETime - Edate) / 604800000);

                if (STime > Sdate && ETime >= Edate) { //跨左
                    d = a - b;
                    e = 1;
                }
                if (STime <= Sdate && ETime < Edate) { //跨右
                    d = Weeks - Math.abs(b);
                    e = Math.abs(b) + 1;
                }
                if (STime <= Sdate && ETime >= Edate) { //居中

                    d = a == 0 ? 1 : a;
                    e = Math.abs(b) + 1;
                }
                if (STime > Sdate && ETime < Edate) {//跨越
                    e = 1;
                    d = 8;
                }
                d = parseInt(d);
                // $("#CC").html(a + "--" + b + "--" + c + "--" + e);
                Data.push({ Id: WeekBillData[i].Id, ItemId: WeekBillData[i].ItemId, x: e, w: d, Title: WeekBillData[i].Title, Content: WeekBillData[i].Content, State: WeekBillData[i].State, BeginDate: WeekBillData[i].BeginDate, EndDate: WeekBillData[i].EndDate })
            }
        }
    }
    function ChangeWeekData() {
        //
        Data = [];
        var Sdate = "";
        var Edate = "";
        var a = "";
        var b = "";
        var c = "";
        var d = "";
        var e = "";
        GetSETime(year, week, 1);
        var STime = new Date(BeginDate.replace(/-/g, "/"));
        var ETime = new Date(EndDate.replace(/-/g, "/"));
        for (var i = 0; i < WeekBillData.length; i++) {
            Sdate = new Date(WeekBillData[i].BeginDate.replace(/-/g, "/"));
            Edate = new Date(WeekBillData[i].EndDate.replace(/-/g, "/"));
            if ((Sdate >= STime && Sdate < ETime) || (Edate > STime && Edate <= ETime) || (Sdate < ETime && Edate > ETime) || (Sdate >= STime && Edate <= ETime)) {
                a = (Edate - Sdate) / 86400000;
                b = (Sdate - STime) / 86400000;
                c = (ETime - Edate) / 86400000;
                if (b <= 0) {
                    if (a - (-b) > 7) {
                        d = 7;
                    }
                    else {
                        d = a - (-b) + 1;
                    }
                    e = 1;
                }
                if (b > 0) {
                    if ((a + b) > 7) {
                        d = 7 - b;
                    }
                    else {
                        d = Math.round(a);
                    }
                    e = b + 1;
                }
                d = parseInt(d);
                Data.push({ Id: WeekBillData[i].Id, ItemId: WeekBillData[i].ItemId, x: e, w: d, Title: WeekBillData[i].Title, Content: WeekBillData[i].Content, State: WeekBillData[i].State, BeginDate: WeekBillData[i].BeginDate, EndDate: WeekBillData[i].EndDate })
            }
        }

    }

    //计算数据坐标
    function GetAutoLayoutData() {

        for (var k = 0; k < 100; k++) {
            tArray[k] = new Array();
            dArray[k] = new Array();
            aArray[k] = new Array();
            for (var j = 0; j < Col; j++) {
                tArray[k][j] = 0;
                dArray[k][j] = 0;
                aArray[k][j] = 0;
            }
        }
        //
        for (var i = 0; i < Col; i++) {

            var s = CreateCoordinate(parseInt(i + 1));
            for (var j = 0; j < s.length; j++) {
                var a = "";
                for (var k = i; k <= parseInt(s[j].x) + parseInt(s[j].w) - 2; k++) {
                    if (a == "") {
                        a = Set(0, k, s, j);
                    }
                    else {
                        tArray[a][k] = s[0].x;
                        aArray[a][k] = s[j].Title;
                        dArray[a][k] = s[j].Id;
                    }
                }
                a = "";
            }

        }
    }
    //获取可放置坐标
    function Set(i, k, s, j) {

        if (tArray[i][k] != 0) {
            i++;
            return Set(i, k, s, j);
        }
        else {
            tArray[i][k] = s[0].x;
            dArray[i][k] = s[j].Id;
            aArray[i][k] = s[j].Title;
            return i;
        }
    }
    //获取指定列数据排序
    function CreateCoordinate(j) {
        var div = [];
        for (var i = 0; i < Data.length; i++) {
            var x = Data[i].x;
            if (x == j) {
                div.push(Data[i]);
            }
        }
        div = div.sort(function (a, b) { return b.w - a.w; });
        return div;
    }
    //根据坐标布局
    function AutoLayout() {

        GetBG_TAb();
        $Tab = $("#Tab");
        $Tab_top = $("#CS_T");
        var html = $Tab.html();
        var THtml = $Tab_top.html();
        html = "";
        THtml = "";
        var Id = "";
        var str = "";
        var s = "";
        var C = 0;
        for (var i = 0; i < tArray.length; i++) {
            for (var k = 1; k <= Col; k++) {
                var width = 0;
                var H = 0;
                for (var j = 0; j < tArray[0].length; j++) {
                    if (tArray[i][j] == k) {
                        width++;
                        str = aArray[i][j];
                        Id = dArray[i][j];
                    }
                }

                if (width != 0) {
                    //
                    s = str;
                    if (Type == 1) {
                        if (width == 1) {
                            str = str.substring(0, 1);
                        }
                        if (width == 2) {
                            str = str.substring(0, 4);
                        }
                        if (width == 3) {
                            str = str.substring(0, 5);
                        }
                        if (width == 4) {
                            str = str.substring(0, 7);
                        }
                        if (FindData(Id).State == "3") {
                            var htm = "<div class='CS_3' Id='L" + Id + "'  ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W + k) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "</div>";
                            html += htm;
                        } else {
                            var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'     ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W + k) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "<div Id='R" + Id + "' class='cs_reight" + FindData(Id).State + "' onmousemove='Change(\"" + Id + "\");'></div></div>";
                            html += htm;
                        }
                         
                    }
                    if (Type == 2) {
                        if (width > 7) {
                            var htm = "<div class='CS_1' Id='L" + Id + "'    ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W) + "px;width:" + parseInt(W * Weeks) + "px;top:" + i * height + "px; '>" + str + "</div>";
                            html += htm;
                        }

                        else {
                            if (k + width > Weeks) {
                                var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'    ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W) + "px;width:" + parseInt(W * width) + "px;top:" + i * height + "px; '>" + str + "</div>";
                                html += htm;
                            }
                            else {
                                if (FindData(Id).State == "3") {
                                    var htm = "<div class='CS_3' Id='L" + Id + "'     ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W + k) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "</div>";
                                    html += htm;
                                } else {
                                    var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'     ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W + k) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "<div Id='R" + Id + "' class='cs_reight" + FindData(Id).State + "' onmousemove='Change(\"" + Id + "\");'></div></div>";
                                    html += htm;
                                }
                                 
                            }

                        }
                    }
                    if (Type == 3) {
                        if (width > 7) {
                            var htm = "<div  class='CS_" + FindData(Id).State + "' Id='T" + Id + "'   ondblclick='OpenContent(\"" + Id + "\")' >" + str + "</div>";
                            $(".CS_T").css({ width: $("#gridTable").width() + "px", });
                            $("#CS_T").css({ backgroundColor: "#FFCE43" })
                            THtml += htm;
                        }
                        else {

                            if (k + width > 7) { 
                                var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'    ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W) + "px;width:" + parseInt(W * width) + "px;top:" + i * height + "px; '>" + str + "</div>";
                                html += htm;
                            }
                            else {
                                if (FindData(Id).State == "3") {
                                    var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'     ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "</div>";
                                    html += htm;
                                } else {
                                    var htm = "<div class='CS_" + FindData(Id).State + "' Id='L" + Id + "'     ondblclick='OpenContent(\"" + Id + "\")' style='left:" + parseInt(k * W - W) + "px;width:" + parseInt(W * width - Gap + width - 3) + "px;top:" + (i - H) * height + "px; '>" + str + "<div Id='R" + Id + "' class='cs_reight" + FindData(Id).State + "' onmousemove='Change(\"" + Id + "\");'></div></div>";
                                    html += htm;
                                }
                            }
                        }
                    }
                }
            }
        }
        //
        $Tab.html(html);
        $Tab_top.html(THtml);
    }
    function OpenContent(Id) {
        var Iddata = $.grep(WeekBillData, function (value) {
            return value.Id == Id;
        });
        var PlanId = Iddata[0].ItemId;
        var BeginDate = FindData(Id).BeginDate;
        var EndDate = FindData(Id).EndDate;
        // var WorkDetail = FindData(Id).WorkDetail;
        var SWeek = FindData(Id).x;
        var Weeks = FindData(Id).w;
        dialogOpen({
            id: "Form",
            title: '详情',
            url: '/PropertyManage/CS_DeviceMaintanenceBussiness/Form?keyValue=' + Id + "&BeginDate=" + BeginDate + "&EndDate=" + EndDate + "&PlanId=" + PlanId + "&SWeek=" + SWeek + "&Weeks=" + Weeks,
            // url: '/WorkManage/Plan/Form?keyValue=' + Id + "&BeginDate=" + BeginDate + "&EndDate=" + EndDate + "&PlanId=" + PlanId + "&WorkDetail=" + WorkDetail + "&SWeek=" + SWeek + "&Weeks=" + Weeks,
            width: "800px",
            height: "500px",
            btn: null,
            callBack: function (iframeId) {
                top.frames["Form"].Delete();
            }
        });
    }
    //删除
    function Del(Id) {

          
        if (FindData(Id).State == 3) {
            top.dialogTop('计划已提交并产生工单，无法删除!', 'error');
            return false;
        }
        if (FindData(Id).State != 3) {
            if (checkedRow(Id)) {
                $.RemoveForm({
                    url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/RemovePlanEntity",
                    param: { keyValue: Id },
                    async: false,
                    success: function (data) {
                        alert(WeekBillData[i].State)
                        if (data == "3") {  
                            if (Type == "1") {
                                ChangeData();
                            }
                            if (Type == "2") {
                                ChangeMonthData();
                            }
                            if (Type == "3") {
                                ChangeWeekData();
                            }
                            GetAutoLayoutData();
                            AutoLayout();
                            GetBG_TAb();
                            return false;

                        }
                    }
                })
            }
        }
        WeekBillData = $.grep(WeekBillData, function (value) {
            return value != FindData(Id);
        });
        if (Type == "1") {
            ChangeData();
        }
        if (Type == "2") {
            ChangeMonthData();
        }
        if (Type == "3") {
            ChangeWeekData();
        }
        GetAutoLayoutData();
        AutoLayout();
        GetBG_TAb();



    }
    function GetSETime(y, w, c) {
        $.ajax({
            type: "GET",
            async: false,
            dataType: "json",
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/GetStimeAndETimeOfWeek?year=" + y + "&Week=" + w + "&Count=" + c,
            success: function (data) {
                BeginDate = data.Stime;
                EndDate = data.Etime;

            }
        });

    }
    function getDayOfWeek(y, w, s, c) {
        $.ajax({
            type: "GET",
            async: false,
            dataType: "json",
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness?year=" + y + "&Week=" + w + "&SDay=" + s + "&Count=" + c,
            success: function (data) {
                BeginDate = data.Stime;
                EndDate = data.Etime;

            }
        });
    }
    //保存
    function btn_Save(s) {
        if (organizeid == "") {
            top.dialogTop('请选择组织机构！', 'error'); 
            return false;
        }


        var SArray = new Array();
        for (var i = 0; i < WeekBillData.length; i++) { 
            if (WeekBillData[i].State != "3") {
                var Json = {
                    Id: WeekBillData[i].Id,
                    ItemId: WeekBillData[i].ItemId,
                    sweek: WeekBillData[i].x,
                    weeks: WeekBillData[i].w,
                    Year: WeekBillData[i].Year,
                    month: WeekBillData[i].Month,
                    BenginTime: WeekBillData[i].BeginDate,
                    EndTime: WeekBillData[i].EndDate,
                    // WorkDetail: WeekBillData[i].WorkDetail,
                    OrganizeId: organizeid
                }
                SArray.push(Json);
            }

        }
        if (SArray.length <= 0) {
            top.dialogTop('没有添加或修改任何数据！', 'error'); 
            return false;
        }
        postData = JSON.stringify(SArray);
        $.ConfirmAjax({
            msg: "注：您确认要保存此操作吗？",
            url: "../../PropertyManage/CS_DeviceMaintanenceBussiness/SubmitData",
            param: { keyValue: postData, Type: s },
            success: function (data) {
                GetWeekBillData();
                if (Type == "1") {
                    ChangeData();
                }
                if (Type == "2") {
                    ChangeMonthData();
                }
                if (Type == "3") {
                    ChangeWeekData();
                }
                GetAutoLayoutData();
                AutoLayout();
                GetBG_TAb();
            }
        });
 

    }

    function ChangeWorkDetail(Id, DeviceListId) {
        FindData(Id).WorkDetail = DeviceListId;
    }



</script>
<style>
    .CS_0 {
        background-color: #fff;
        line-height: 20px;
        width: 200px;
        position: absolute;
        border: #5BC0DE solid 3px; 
        cursor: pointer;
        border-radius:3px;
    }

    .CS_1 {
        background-color: #fff;
        line-height: 20px;
        width: 200px;
        position: absolute;
        border: #5CB85C solid 3px;
        cursor: pointer;
        border-radius: 3px;
    }

    .CS_2 {
        background-color: #fff;
        line-height: 20px;
        width: 200px;
        position: absolute;
        border: #CE746C solid 3px;
        cursor: pointer;
        border-radius: 3px;
    }

    .CS_3 {
        background-color: #fff;
        line-height: 20px;
        width: 200px;
        position: absolute;
        border: #B59A8F solid 3px;
        border-radius: 3px;
        cursor: pointer;
    }

    .CS_T {
        background-color: #fff;
        line-height: 20px;
        border: #23B7E5 solid 3px;
        margin: 3px;
    }

    #CS_T {
        background-color: #FCF8E3;
        position: absolute;
        z-index: 99999;
        top: 200px;
        cursor: pointer;
    }

    .cs_reight0 {
        float: right;
        background-color: #5BC0DE;
        width: 3px;
        height: 20px;
        margin-right: -3px;
        cursor: ew-resize;
    }

    .cs_reight1 {
        float: right;
        background-color: #5CB85C;
        width: 3px;
        height: 20px;
        margin-right: -3px;
        cursor: ew-resize;
    }

    .cs_reight2 {
        float: right;
        background-color: #CE746C;
        width: 3px;
        height: 20px;
        margin-right: -3px;
        cursor: ew-resize;
    }

    #gridTable tr td {
        padding: 0px;
    }

    .cs_dv {
        border-right: solid 1px #CCCCCC;
        position: relative;
        height: 750px;
    }

    #Tab {
        position: absolute;
        left: 0px;
        top: 0px;
        width: 100px;
    }

    #CS {
        background: #23B7E5;
        line-height: 30px;
        text-align: center;
    }

        #CS:hover {
            cursor: pointer;
        }

    #HZ {
        position: absolute;
        background: #23B7E5;
        line-height: 30px;
        text-align: center;
        width: 190px;
        z-index: 9999;
        border-radius: 3px;
        cursor: pointer;
        visibility: visible;
        cursor: pointer;
    }
</style>
<div style="position:absolute;top:0; right:150px;z-index:1000;background:rgba(0, 0, 0, 0.1);padding:10px;border-radius:0px 0px 5px 5px;">
    <div class="btn-group">
        <a class="btn btn-success" id="btn_Save" data-toggle="dropdown" onclick="btn_Save(1);">保存</a>
        <a class="btn btn-info" id="btn_Submit" data-toggle="dropdown" onclick="btn_Save(2);">提交</a>
    </div>
</div>
<div class="ui-layout" onselectstart="return false;" id="layout" style="height: 100%; width: 100%;">


    <div class="ui-layout-west">

        <div class="west-Panel" style="overflow-y: auto; ">

            <div class="panel-Title" style="padding:0px; text-align:center;">工作项目列表</div>
            <div id="itemTree1">
            </div>
        </div>
    </div>
    <div class="ui-layout-center">

        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table id="queryCondition">
                        <tr style="padding-left: 10px;">
                            <td>机构筛选</td>
                            <td style="padding-left: 5px;"><div style="width:180px;" id="OrganizeId" type="selectTree" class="ui-select"></div></td>
                            <td><div id="CC"></div></td>
                            <td>
                                <a id="btn_Search" onclick="btn_up()" class="btn btn-default"><i class="fa fa-chevron-left"></i></a>
                            </td>
                            <td><div id="Time" style="font-size:18px;padding:0px 5px; width:120px; text-align:center;"></div></td>
                            <td>
                                <a id="btn_Search" onclick="btn_down()" class="btn btn-default"><i class="fa fa-chevron-right"></i></a>
                            </td>

                        </tr>
                    </table>
                </div>

                <div class="toolbar">
                    <div id="time_see" class="btn-group">
                        <a class="btn btn-default  " data-value="1">年</a>
                        <a class="btn btn-default active " data-value="2">月</a>
                        <a class="btn btn-default  " data-value="3">周</a>
                    </div>
                </div>
               
            </div>
            <div style="height:30px;">
                <div style="display: inline-block;margin-left: 10px;"><i style="padding-right: 5px; color: #5BC0DE;" class="fa fa-flag"></i><span>新增中</span></div>
                <div style="display: inline-block;margin-left: 10px;"><i style="padding-right: 5px; color: #5CB85C;" class="fa fa-flag"></i><span>已保存</span></div>
                <div style="display: inline-block;margin-left: 10px;"><i style="padding-right: 5px; color: #CE746C;" class="fa fa-flag"></i><span>修改中</span></div>
                <div style="display: inline-block;margin-left: 10px;"><i style="padding-right: 5px; color: #B59A8F;" class="fa fa-flag"></i><span>已提交</span></div>
            </div>      
                <div class="gridPanel">
                    <table id="gridTable"></table>

                </div>

            </div>

    </div>

</div>

<div id="HZ" onselectstart="return false;"></div>
<div id="CS_T" onselectstart="return false;"></div>
<div id="Tab" onselectstart="return false;">

</div>


